// --------------------------------------------------------------------
    // UDP服务端实现
// --------------------------------------------------------------------

#include "userver.hpp"
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

namespace ymbxSock{

udpSrv::udpSrv(uint16_t port):_port(port){
    CreateLog("udpserver");
    _sock.Socket("udp");
    _sock.Bind("",_port);
}
void udpSrv::Start(Handler handler){
    std::cout<<"server start>>>\n";
    while(1){
        std::string mes;
        std::string ip;
        uint16_t port;
        _sock.Recvfrom(&ip,&port,&mes);
        std::cout<<"接收到来自"<<ip<<":"<<port<<"的消息>>>"<<mes<<std::endl;
        sleep(1);
        std::string resp;
        //业务处理
        handler(mes,&resp);
        _sock.Sendto(ip,port,resp);
    }
}
udpSrv::~udpSrv(){
    _sock.Close();
}
}

void working(const std::string& req,std::string* resp){
    *resp=req;
}

int main(int argc,char* argv[]){
    if(argc!=2){
        ymbxSock::SrvPromote();
        abort();
    }
    uint16_t port=std::atoi(argv[1]);
    ymbxSock::udpSrv srv(port);
    srv.Start(working);
    return 0;
}